package org.dromara.yuanshidian.interest;



import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class InterestCalcLogicUtil {
    static  ThreadLocal<InterestCalcLocal> interestCalcLocal = ThreadLocal.withInitial(InterestCalcLocal::new);
     public static InterestCalcLocal getInterestCalcLocal() {
        return interestCalcLocal.get();
    }

    public static  Double calcInterest(List<PaymentInfo> paymentInfo, Date calcPerid, Map<Integer,Double> reteMap){
        interestCalcLocal.get().initInterestcalcLocal(calcPerid,reteMap);
        Double res = paymentInfo.stream().map(InterestCalcLogicUtil::calcPaymentInterest).reduce(Double::sum).get();
        return res;
    }

    public static  Double calcPaymentInterest(PaymentInfo paymentInfo){
        Double res = 0d;
        int startYear = DateUtil.year(paymentInfo.getPeriod());
        int yearIndex = startYear;
        int endYear = DateUtil.year(interestCalcLocal.get().getCalcPeriod());
        while (yearIndex <= endYear){
//            单利
            res += interestCalcLocal.get().caclYearInterest(paymentInfo.getAmount(),yearIndex,paymentInfo.getPeriod());
//            复利
//            res += interestCalcLocal.get().caclYearInterest(startYear ==yearIndex ?paymentInfo.getAmount():(res + paymentInfo.getAmount()),yearIndex,paymentInfo.getPeriod());
            yearIndex++;
        }
        return  res;
    }
}
